package utils;

import gui.GUI;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.GregorianCalendar;

import javax.imageio.ImageIO;

import main.Enemies;
import main.HP;
import main.Main;

import static main.Constants.*;
import static main.Main.*;

import static utils.Images.*;

public final class Utils {

	static final ArrayList<Integer> COLOR_RANGE;
	static final ArrayList<Integer> COLOR_RANGE_RED;
	static {
		COLOR_RANGE = new ArrayList<Integer>();
		COLOR_RANGE_RED = new ArrayList<Integer>();
		// COLOR_RANGE.add(new Color(81, 21, 17).getRGB()); // shershen'
		// COLOR_RANGE.add(new Color(100, 50, 70).getRGB()); // shershen'
		// COLOR_RANGE.add(new Color(126, 6, 11).getRGB()); // shershen'
		int i;
		for (i = 25; i <= 255; i++)
			COLOR_RANGE_RED.add(new Color(i, 0, 0).getRGB());
		for (i = 0; i <= 20; i++) {
			COLOR_RANGE.add(new Color(190 + i, 190 + i, 190 + i).getRGB()); // gray
																			// for
																			// perc
																			// and
																			// shersh
		}
	}

	static public boolean compare(BufferedImage im1, BufferedImage im2) {
		for (int x = 0; x < im1.getWidth() / 2; x++)
			for (int y = 0; y < im1.getHeight() / 2; y++)
				if (im1.getRGB(x, y) != im2.getRGB(x, y))
					return false;
		return true;
	}

	public static void main(String[] args) throws Exception {
		// createSnapshot(HEALTH, HEALTH_NAME);
		//createSnapshot(LONG_HEALTH, LONG_HEALTH_NAME);
		createSnapshot(ATTACK, ATTACK_NAME_RED);
		// createSnapshot(THERD_LEFT, fileName)
		// wantCarrots();
	}

	static public BufferedImage createSnapshot(Rectangle rec, String fileName) throws IOException, AWTException {
		Robot robo = new Robot();
		BufferedImage image = robo.createScreenCapture(rec);

		ImageIO.write(image, "png", new File(fileName));
		return image;
	}

	static public Point target(Enemies en, BufferedImage image) {
		for (int j = image.getHeight() - 1; j >= 0; j--)
			for (int i = image.getWidth() - 1; i >= 0; i--) {
				int color = image.getRGB(i, j);
				if (COLOR_RANGE.contains(color))
					return new Point((int) en.enRect.getMinX() + i, (int) en.enRect.getMinY() + j);
			}

		return null;

	}

	public static void LKM(int delay) {
		robo.mousePress(InputEvent.BUTTON1_MASK);
		robo.delay(delay);
		robo.mouseRelease(InputEvent.BUTTON1_MASK);
	}

	public static boolean isReadyHealth(){
		BufferedImage imageCurrent = robo.createScreenCapture(HEALTH);
		return Utils.compare(imageCurrent, imageFullHealth);
	}

	public static boolean isReadyAttack(){
		BufferedImage imageCurrent = robo.createScreenCapture(ATTACK);
		return Utils.compare(imageCurrent, imageAttackRed) || Utils.compare(imageCurrent, imageAttackDark);
	}

	public static boolean isAttacBegan(){
		BufferedImage imageCurrent = robo.createScreenCapture(SHIELD);
		return Utils.compare(imageCurrent, imageShield);
	}

	public static Rectangle isReadyClose(){
		BufferedImage imageCurrentGift = robo.createScreenCapture(CLOSE_BUTTLE_GIFT);
		BufferedImage imageCurrentNo = robo.createScreenCapture(CLOSE_BUTTLE_NO_GIFT);
		if (Utils.compare(imageCurrentGift, imageCloseGift)) {
			return CLOSE_BUTTLE_GIFT;
		}
		if (Utils.compare(imageCurrentNo, imageCloseNo)) {
			return CLOSE_BUTTLE_NO_GIFT;
		}
		return null;
	}

	public static boolean useCell(Rectangle rec, int val) throws InterruptedException{

		if (currentHealth() < val) {
			eatSmth(rec);
			println("MNJM - MNJM");
			return true;
		} else {
			println("DONT WANT EAT");
			return false;
		}

	}

	private static void eatSmth(Rectangle rec) throws InterruptedException {
		robo.mouseMove((int) rec.getMinX() + 7, (int) rec.getMinY() + 10);
		Thread.sleep(300);
		LKM(50);
		robo.mouseMove(960, 540);
	}

	public static Point searcByMovie(Rectangle enRect, int r) throws InterruptedException{
		BufferedImage im1 = robo.createScreenCapture( enRect);
		Thread.sleep(600);
		BufferedImage im2 = robo.createScreenCapture( enRect);

		int razmer = r;
		int h = im1.getHeight();
		int w = im1.getWidth();

		Integer[][] array = new Integer[w][h];

		for (int y = 0; y < h; y++) {
			for (int x = 0; x < w; x++) {
				if (im1.getRGB(x, y) != im2.getRGB(x, y))
					array[x][y] = 1;
				else
					array[x][y] = 0;
			}
		}
		for (int y = razmer; y < h - razmer; y++) {
			for (int x = razmer; x < w - razmer; x++) {
				if (searchFunc(razmer, array, x, y)) {
					// array[x][y] = 5;
					return new Point((int)  enRect.getMinX() + x, (int)  enRect.getMinY() + y);
				}
			}
		}
		return null;
	}

	private static boolean searchFunc(int razmer, Integer[][] array, int xx, int yy) {

		if (array[xx][yy] == 0)
			return false;

		for (int x = 0; x < razmer; x++) {
			if (array[xx - x][yy] != 1)
				return false;
			if (array[xx + x][yy] != 1)
				return false;
		}

		for (int y = 0; y < razmer; y++) {
			if (array[xx][yy + y] != 1)
				return false;
			if (array[xx][yy - y] != 1)
				return false;
		}
		return true;
	}

	public static void println(String str) {
		String s = GUI.textWindow.getText();
		int i=0;
		for(char c : s.toCharArray())
			if (c == "\n".toCharArray()[0]) i++; 
		if (i>2) s = s.substring(s.indexOf("\n")+1, s.length());
		GUI.textWindow.setText(
				s+"\n" +
				str + " " + currentTime());
		System.out.println(str + " " + currentTime());
	}

	// public static void drinkHP() {
	// if (!compare(robo.createScreenCapture(THERD_LEFT), image3L)) {
	// println("NOT HAVE HP el' !!!");
	// }
	//
	// BufferedImage im1 = robo.createScreenCapture(LONG_HEALTH);
	//
	// for (int i = 15; i < LONG_HEALTH.width; i++) {
	// if (im1.getRGB(i, 6) != imageLongFullHealth.getRGB(i, 6))
	// if (maxHP - (int) ((i + 0.0) / 100 * maxHP) > 250) {
	// eatSmth();
	// System.out.println(i + " Demadge = " + (maxHP - (int) ((i + 0.0) / 100 *
	// maxHP)));
	// System.out.println(" Current HP= " + ((i + 0.0) / 100 * maxHP));
	// println("MNJM - MNJM");
	// return;
	// } else {
	// System.out.println(i + " Demadge = " + (maxHP - (int) ((i + 0.0) / 100 *
	// maxHP)));
	// System.out.println(" Current HP= " + ((i + 0.0) / 100 * maxHP));
	// println("DONT WANT CARROTS");
	// return;
	// }
	// }
	// println("Pic not woved !");
	// }

	public static int currentHealth() throws InterruptedException{

		BufferedImage im1 = robo.createScreenCapture(LONG_HEALTH);
		Thread.sleep(300);

		for (int i = LONG_HEALTH.width - 1; i > 0; i--)
			for (int j = 0; j < LONG_HEALTH.height; j++)

			{
				if (COLOR_RANGE_RED.contains(im1.getRGB(i, j))) {
//					println("width = " + LONG_HEALTH.width + "");
//					println("point num = " + i + "");
//					println("hp = " + ((i + 1) * (maxHP) / (LONG_HEALTH.width)));
					return ((i + 1) * (HP.maxHP) / (LONG_HEALTH.width));
				}
			}
		return HP.maxHP;
	}
	
	public static void refresh() throws InterruptedException {
		Main.robo.keyPress(KeyEvent.VK_F5);
		sleep(25000);
		Utils.LKM(50);
		Main.robo.mouseWheel(MAX_PRIORITY);
		Main.robo.mouseWheel(MAX_PRIORITY);

	}
	
	public static String currentTime()
	{
		String str = GregorianCalendar.getInstance().getTime().toString();
		return str.substring(4, str.length()-9);
	}

}
